// pages/my_credit/my_credit.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dist/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/dist/toast/toast';
import toast from '../../miniprogram_npm/@vant/weapp/dist/toast/toast';
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    credit: 0,
    chargeNum: '',
    withdrawNum: ''
  },

  chargeTap() {
    Dialog.alert({
      message: '小程序处于测试阶段，\n充值和提现均不涉及真实资金交易',
    })
    .then(() => {
      Dialog.confirm({
          selector: "#charge-input",
          title: "积分充值",
        })
        .then(() => {
          console.log(this.data.chargeNum)
          wx.cloud.callFunction({
            name: "changeUserData",
            data: {
              action: "addCredit",
              openid: app.globalData.openid,
              num: Number(this.data.chargeNum)
            },
            success: (res) => {
              this.setData({
                chargeNum: ''
              })
              Toast.success("充值成功")
              this.onShow()
            },
            fail: (res) => {
              this.setData({
                chargeNum: ''
              })
              Toast.fail("网络不给力……")
              console.log(res)
            },
          })
        })
        .catch((err) => {
          console.log(err)
        })
    });
  },

  withdrawTap() {
    Dialog.alert({
      message: '小程序处于测试阶段，\n充值和提现均不涉及真实资金交易',
    })
    .then(() => {
      Dialog.confirm({
        selector: "#withdraw-input",
        title: "积分提现",
      })
      .then(() => {
        console.log(this.data.withdrawNum)
        if(this.data.credit<this.data.withdrawNum){
          Toast.fail("提现积分超过账户积分")
          return
        }
        wx.cloud.callFunction({
          name: "changeUserData",
          data: {
            action: "subCredit",
            openid: app.globalData.openid,
            num: Number(this.data.withdrawNum)
          },
          success: (res) => {
            this.setData({
              withdrawNum: ''
            })
            Toast.success("提现成功")
            this.onShow()
          },
          fail: (res) => {
            this.setData({
              withdrawNum: ''
            })
            Toast.fail("网络不给力……")
            console.log(res)
          },
        })
      })
      .catch((err) => {
        console.log(err)
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.cloud.callFunction({
      name: "getUserData",
      data: {
        action: "getCredit",
        openid: app.globalData.openid
      },
      success: (res) => {
        console.log(res)
        this.setData({
          credit: res.result
        })
      },
      fail: (res) => {
        wx.showToast({
          icon: "none",
          title: '网络不给力…',
        })
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})